package adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;

import java.util.ArrayList;

import bean.Second_Car;
import utils.MyStoreDao;
import utils.MyUrl;
import utils.RequestQueueUtils;
import zhang.automobileservice.R;

/**
 * Created by Administrator on 2016/8/4.
 */
public class SecondCarAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<Second_Car> arrayList;
    private ArrayList<String> list;

    public SecondCarAdapter(Context context, ArrayList<Second_Car> arrayList,ArrayList<String> list) {
        this.context = context;
        this.arrayList = arrayList;
        this.list = list;
    }
    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    MyStoreDao dao;
    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.secondcar_listitem, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Second_Car second_car = arrayList.get(position);
        String [] img = second_car.getImage().split(",");
        String url = MyUrl.Font_Url+MyUrl.CarImage;
        RequestQueueUtils.exectueNetWorkImageView(context,url+img[0],viewHolder.iv_image);
        viewHolder.tv_title.setText(second_car.getTitle().trim());
        viewHolder.tv_price.setText(second_car.getPrice()+"万");
        viewHolder.tv_tab.setText(second_car.getTab());
        viewHolder.tv_data.setText(second_car.getKm()+"万公里/"+second_car.getBuyDate().substring(0,4)+"年/个人");
        viewHolder.tv_tab01.setVisibility(View.INVISIBLE);
        dao = new MyStoreDao(context);
        viewHolder.tv_delect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dao.delect(list.get(position));
                list.remove(position);
                arrayList.remove(position);
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    public static class ViewHolder {
        public View rootView;
        public NetworkImageView iv_image;
        public TextView tv_title;
        public TextView tv_data;
        public TextView tv_price;
        public TextView tv_tab;
        public TextView tv_tab01;
        public TextView tv_delect;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.iv_image = (NetworkImageView) rootView.findViewById(R.id.iv_image);
            this.tv_title = (TextView) rootView.findViewById(R.id.tv_title);
            this.tv_data = (TextView) rootView.findViewById(R.id.tv_data);
            this.tv_price = (TextView) rootView.findViewById(R.id.tv_price);
            this.tv_tab = (TextView) rootView.findViewById(R.id.tv_tab);
            this.tv_tab01 = (TextView) rootView.findViewById(R.id.tv_tab01);
            this.tv_delect = (TextView) rootView.findViewById(R.id.tv_delect);
        }

    }
}
